package cn.doitedu.rtmk.data_moni;

import org.roaringbitmap.RoaringBitmap;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @Author: deep as the sea
 * @Site: <a href="www.51doit.com">多易教育</a>
 * @QQ: 657270652
 * @Date: 2023/4/14
 * @Desc: 学大数据，到多易教育
 *   规则元信息  数据模拟器
 **/
public class RuleMetaDataMoni {

    public static void main(String[] args) throws SQLException, IOException {

        // String paramJson1 = "{\"rule_id\":1,\"rule_model_id\":1,\"triggerEventId\":\"ad_click\",\"triggerEventPropertyName\":\"ad_id\",\"triggerEventPropertyValue\":\"ad001\"}";
        // insertRule(new int[]{1,2,3,5,7},1,1,paramJson1);

        // String paramJson2 = "{\"rule_id\":2,\"rule_model_id\":1,\"triggerEventId\":\"page_load\",\"triggerEventPropertyName\":\"url\",\"triggerEventPropertyValue\":\"/page/001\"}";
        // insertRule(new int[]{1,2,3},2,1,paramJson2);




        String paramJson3 = "{\n" +
                "  \"rule_id\":3,\n" +
                "  \"rule_model_id\":2,\n" +
                "  \"static_profile_conditions\":[\n" +
                "    {\"tag_name\":\"TAG0101\",\"compare\":\">\",\"tag_value\":[200]},\n" +
                "    {\"tag_name\":\"TAG0102\",\"compare\":\"bt\",\"tag_value\":[20,30]},\n" +
                "    {\"tag_name\":\"TAG0103\",\"compare\":\"=\",\"tag_value\":[\"male\"]}\n" +
                "  ],\n" +
                "  \"dynamic_profile_conditions\":{\n" +
                "    \"event_id\":\"add_cart\",\n" +
                "    \"event_property_name\":\"item_id\",\n" +
                "    \"event_property_value\":\"item001\",\n" +
                "    \"minCount\":3\n" +
                "  },\n" +
                "  \"trigger_condition\":{\n" +
                "    \"event_id\":\"share\",\n" +
                "    \"event_property_name\":\"url\",\n" +
                "    \"event_property_value\":\"/page/002\"\n" +
                "  }\n" +
                "\n" +
                "}";

        insertRule(new int[]{2,3,4,5,6,7},3,2,paramJson3);


    }



    public static void insertRule(int[] userIds,int ruleId,int ruleModelId,String ruleParamJson)  throws SQLException, IOException {

        // 模拟 web系统从 elastic-search 中，根据本规则的静态画像圈选条件
        // 查到了满足该规则的人群为： 1,3,5,7用户

        // 并将这些人的id，装入一个bitmap对象中
        RoaringBitmap bitmap = RoaringBitmap.bitmapOf();
        bitmap.add(userIds);

        // 将装好人群id的bitmap序列化成一个字节数组
        ByteArrayOutputStream baOut = new ByteArrayOutputStream();
        DataOutputStream dOut = new DataOutputStream(baOut);
        bitmap.serialize(dOut);

        byte[] bitmapBytes = baOut.toByteArray();


        /**
         * 模拟web系统，创建jdbc连接，将上面的规则数据，写入mysql的rule_meta表
         */
        Connection conn = DriverManager.getConnection("jdbc:mysql://doitedu:3306/doit37", "root", "root");

        PreparedStatement pst = conn.prepareStatement("insert into rule_meta_info values(?,?,?,?)");
         //设置本规则的规则id为： 1
         pst.setLong(1,ruleId);
         pst.setInt(2,ruleModelId);
        // // 设置本规则的预圈选人群bitmap
         pst.setBytes(3,bitmapBytes);
         pst.setString(4,ruleParamJson);
         pst.execute();

        // 关闭连接资源 ,
        pst.close();
        conn.close();


    }
}
